/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.core;

import java.util.List;

import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.solrdimension.config.DimensionConfigWrapper;
import com.google.code.solrdimension.config.dimension.DimensionConfig;
import com.google.code.solrdimension.config.dimensionvalue.AbstractDimensionValueConfig;
import com.google.code.solrdimension.indexmodifiers.IndexModService;
import com.google.code.solrdimension.parsers.exception.SolrDimensionException;

/**
 * This is the core Class for constructing a SolrInputDocument for SOLR and
 * contains logic to create new fields depending on the SolrDimensionConfig
 * 
 * @author Michael Chan
 */
public class DimensionInputDocument {
    
    private static Logger logger = LoggerFactory.getLogger(DimensionInputDocument.class);
    
    
    private IndexModService indexModService;
    

    private DimensionConfigWrapper solrDimensionConfig;

    private SolrInputDocument solrInputDocument;

    
    public DimensionInputDocument(SolrInputDocument inputdoc, DimensionConfigWrapper solrDimensionConfig, IndexModService indexModService) throws SolrDimensionException{
        this.solrInputDocument = inputdoc;
        this.indexModService = indexModService;
        this.solrDimensionConfig = solrDimensionConfig;
        
        if (this.solrDimensionConfig == null){
            throw new SolrDimensionException("Problem retrieving SolrDimensionConfig");
        }
    }

    public SolrInputDocument getSolrInputDocument() {
        return solrInputDocument;
    }

    public void setSolrInputDocument(SolrInputDocument solrInputDocument) {
        this.solrInputDocument = solrInputDocument;
    }

    /**
     * This basically calls the
     * {@link SolrInputDocument#addField(String, Object)} method but with additional
     * processing with the {@link IndexModService}
     * 
     * @param fieldName
     * @param obj
     */
    public void addField(String fieldName, Object obj) {
        
      //index the field anyway
        solrInputDocument.addField(fieldName, obj);

        //If the field matches 
        if (this.solrDimensionConfig.getSolrFieldsToModify().contains(fieldName)) {
            
            logger.debug("FieldName: " + fieldName + " matches the list of " + solrDimensionConfig.getSolrFieldsToModify());
            
            List<DimensionConfig> dimensionConfigs = this.solrDimensionConfig.getDimensionConfigsBySolrField(fieldName, true);
            
            if (dimensionConfigs != null && !dimensionConfigs.isEmpty()){
                
                for (DimensionConfig dimConfig : dimensionConfigs){
                    
                    for (AbstractDimensionValueConfig dimValCofig : dimConfig.getDimensionValueConfigs()){
                        
                        if (indexModService.getIndexModAction(dimValCofig) != null ){
                            
                            indexModService.modifyIndex(dimValCofig, this.solrInputDocument, obj, fieldName);
                            
                        } else {
                            // if there isn't one action for a dimValconfig, we can break
                            // because a dimensionConfig contains only a list of dimValConfig of the same type 
                            break; 
                            
                        }
                        
                    }
                    
                }
                
            }
            
        } else {
            
            logger.debug("Field: " + fieldName + " is ignored by SolrDimension since this is not in the configurations");
            
        }
        
    }

    public void setSolrDimensionConfig(DimensionConfigWrapper solrDimensionConfig) {
        this.solrDimensionConfig = solrDimensionConfig;
    }

}
